package com.buanalintas.sandals.pos.reportsale;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Bynga;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Formatter;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.Template;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.formatter.DateFormatter;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.bynga.formatter.NumberFormatter;
import com.buanalintas.bynga.generator.PageList;
import com.buanalintas.sandals.pos.entities.PosMasterSales;
import com.buanalintas.sandals.pos.entities.PosTransactionSale;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;

public class ReportSale extends Page {

    private Field saleId;
    private Field saleUnitId;
    private Field saleUserId;
    private Field saleNumber;
    private Field saleSubtotal;
    private Field saleDiscountPercent;
    private Field saleDiscountNominal;
    private Field saleTotal;
    private Field saleDate;
    private Field saleTimestamp;
    private Formatter format;
    private static String MESSAGE_DAILY = "<!--MESSAGE-DAILY-->";
    private static String MESSAGE_WEEKLY = "<!--MESSAGE-WEEKLY-->";
    private static String MESSAGE_MONTHLY_YEAR = "<!--MESSAGE-MONTHLY-YEAR-->";
    private static String MESSAGE_MONTHLY_MONTH = "<!--MESSAGE-MONTHLY-MONTH-->";
    private static String MESSAGE_YEARLY = "<!--MESSAGE-YEARLY-->";
    private static String MESSAGE_CUSTOM = "<!--MESSAGE-CUSTOM-->";
    private static String SELECT_MODE = "<!--SELECT-MODE-->";
    private static String SELECT_MODE_ONCHANGE = "<!--SELECT-MODE-ONCHANGE-->";
    private static String SELECT_VALUES = "<!--SELECT-VALUES-->";
    private static String SELECT_FILTER = "<!--SELECT-FILTER-->";
    private static String SELECT_FILTER_VALUES = "<!--SELECT-FILTER-VALUES-->";
    private static String MODE_DAILY = "MODE-DAILY";
    private static String DAILY_SELECT_DATE = "<!--DAILY-SELECT-DATE-->";
    private static String DAILY_DATE_VALUE = "<!--DAILY-DATE-VALUE-->";
    private static String MODE_WEEKLY = "MODE-WEEKLY";
    private static String WEEKLY_START_DATE_VALUE = "<!--WEEKLY-START-DATE-VALUE-->";
    private static String WEEKLY_SELECT_START_DATE = "<!--WEEKLY-SELECT-START-DATE-->";
    private static String MODE_MONTHLY = "MODE-MONTHLY";
    private static String MONTHLY_SELECT_YEAR = "<!--MONTHLY-SELECT-YEAR-->";
    private static String MONTHLY_SELECT_YEAR_VALUES = "<!--MONTHLY-SELECT-YEAR-VALUES-->";
    private static String MONTHLY_SELECT_MONTH = "<!--MONTHLY-SELECT-MONTH-->";
    private static String MONTHLY_SELECT_MONTH_VALUES = "<!--MONTHLY-SELECT-MONTH-VALUES-->";
    private static String MODE_YEARLY = "MODE-YEARLY";
    private static String YEARLY_SELECT_YEAR = "<!--YEARLY-SELECT-YEAR-->";
    private static String YEARLY_SELECT_YEAR_VALUES = "<!--YEARLY-SELECT-YEAR-VALUES-->";
    private static String MODE_CUSTOM = "MODE-CUSTOM";
    private static String CUSTOM_SELECT_START_DATE = "<!--CUSTOM-SELECT-START-DATE-->";
    private static String CUSTOM_START_DATE_VALUE = "<!--CUSTOM-START-DATE-VALUE-->";
    private static String CUSTOM_SELECT_END_DATE = "<!--CUSTOM-SELECT-END-DATE-->";
    private static String CUSTOM_END_DATE_VALUE = "<!--CUSTOM-END-DATE-VALUE-->";
    private static String BUTTON_SEARCH = "<!--BUTTON-SEARCH-->";
    private static String LIST_DATA = "<!--LIST-DATA-->";
    private static String RECORD = "<!--RECORD-->";
    private static String WARNING = "<!--WARNING-->";
    private static String TOTAL = "TOTAL";
    private static String TOTAL_CAPTION = "<!--TOTAL-CAPTION-->";
    private static String TOTAL_TEXT = "<!--TOTAL-TEXT-->";
    private static String RECAP_EXPORT_LINK = "<!--RECAP-EXPORT-LINK-->";
    private static String RECAP_EXPORT = "RECAP-EXPORT";
    private Action actionShow = new Action("show", "Show report sale");
    private Action actionExport = new Action("export", "Export report sale");

    public ReportSale() {

        super();
        setTheClass(PosTransactionSale.class);

        saleId = new Field();
        saleId.setName("saleId");
        saleId.setCaption("id");
        saleId.setListProperties("");
        saleId.setInputProperties("");
        getFields().add(saleId);

        saleUnitId = new Field();
        saleUnitId.setName("saleUnitId");
        saleUnitId.setCaption("unit id");
        saleUnitId.setListProperties("");
        saleUnitId.setInputProperties("");
        getFields().add(saleUnitId);

        saleUserId = new Field();
        saleUserId.setName("saleUserId");
        saleUserId.setCaption("user id");
        saleUserId.setListProperties("");
        saleUserId.setInputProperties("");
        getFields().add(saleUserId);

        saleNumber = new Field();
        saleNumber.setName("saleNumber");
        saleNumber.setCaption("number");
        saleNumber.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        saleNumber.setInputProperties(" size=\"20\" readonly");
        saleNumber.setRequired(true);
        getFields().add(saleNumber);

        saleSubtotal = new Field();
        saleSubtotal.setName("saleSubtotal");
        saleSubtotal.setCaption("subtotal");
        saleSubtotal.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        saleSubtotal.setInputProperties(" size=\"8\"");
        saleSubtotal.setRequired(true);
        saleSubtotal.setFormater(MoneyFormatter.getInstance());
        getFields().add(saleSubtotal);

        saleDiscountPercent = new Field();
        saleDiscountPercent.setName("saleDiscountPercent");
        saleDiscountPercent.setCaption("discount percent");
        saleDiscountPercent.setListProperties(" align=\"right\" width=\"15%\" nowrap");
        saleDiscountPercent.setInputProperties(" size=\"8\"");
        getFields().add(saleDiscountPercent);

        saleDiscountNominal = new Field();
        saleDiscountNominal.setName("saleDiscountNominal");
        saleDiscountNominal.setCaption("discount nominal");
        saleDiscountNominal.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        saleDiscountNominal.setInputProperties(" size=\"8\"");
        saleDiscountNominal.setFormater(MoneyFormatter.getInstance());
        getFields().add(saleDiscountNominal);

        saleTotal = new Field();
        saleTotal.setName("saleTotal");
        saleTotal.setCaption("total");
        saleTotal.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        saleTotal.setInputProperties(" size=\"8\"");
        saleTotal.setRequired(true);
        saleTotal.setFormater(MoneyFormatter.getInstance());
        getFields().add(saleTotal);

        saleDate = new Field();
        saleDate.setName("saleDate");
        saleDate.setCaption("date");
        saleDate.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        saleDate.setInputProperties(" size=\"8\" readonly");
        saleDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(saleDate);

        saleTimestamp = new Field();
        saleTimestamp.setName("saleTimestamp");
        saleTimestamp.setCaption("timestamp");
        saleTimestamp.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        saleTimestamp.setInputProperties(" size=\"8\" readonly");
        saleTimestamp.setDefaultValue(Utility.getTimestamp());
        getFields().add(saleTimestamp);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(saleDate);
        getFieldsAdd().add(saleNumber);
        getFieldsAdd().add(saleDiscountPercent);
        getFieldsAdd().add(saleDiscountNominal);

        getFieldsEdit().clear();
        getFieldsEdit().add(saleNumber);
        getFieldsEdit().add(saleDate);
        getFieldsEdit().add(saleDiscountPercent);
        getFieldsEdit().add(saleDiscountNominal);

        getFieldsList().clear();
        getFieldsList().add(saleNumber);
        getFieldsList().add(saleDate);
        getFieldsList().add(saleSubtotal);
        getFieldsList().add(saleDiscountPercent);
        getFieldsList().add(saleDiscountNominal);
        getFieldsList().add(saleTotal);

        getFieldsView().clear();
        getFieldsView().add(saleNumber);
        getFieldsView().add(saleSubtotal);
        getFieldsView().add(saleDiscountPercent);
        getFieldsView().add(saleDiscountNominal);
        getFieldsView().add(saleTotal);
        getFieldsView().add(saleTimestamp);

        getActions().add(actionShow);
        getActions().add(actionExport);
        setExportList(getClass().getResourceAsStream("ReportSaleList.jasper"));
        setExport(getClass().getResourceAsStream("ReportSale.jasper"));
    }

    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        saleDate.setFormater(DateFormatter.getInstanceDate(this));
        format = DateFormatter.getInstanceDateTime(this);
    }

    @Override
    public boolean isSearchVisible() {
        return false;
    }

    @Override
    public boolean isTitleVisible() {
        return false;
    }

    @Override
    public StringBuffer process() {

        if (getAction().equals(actionExport)) {
            try {
                updateExportParams();
                if ("2".equals(getCurrentUser().getUserExport())) {
                    getResponse().setHeader("Content-Type", "application/vnd.ms-excel ");
                } else {
                    getResponse().setHeader("Content-Type", "application/pdf");
                }
                JRDataSource datasource = null;
                String filter = Utility.getParameterEmpty(getRequest(), "filter");
                String mode = getRequest().getParameter("mode");
                EntityManager em = getController().getEntityManager();
                PosMasterSales spg = null;
                try {
                    spg = (PosMasterSales) getEntityManager().createQuery("SELECT t FROM PosMasterSales t WHERE t.salesName=" + Utility.getPostParameterEmpty(this, "filter")).setHint(Persistence.REFRESH_HINT, true).getSingleResult();

                } catch (Exception e) {
                }
                if ("1".equals(mode)) {
                    String filterDate = Utility.getParameterEmpty(getRequest(), "recapDate");
                    getExportParams().put("title", Dictionary.getValue(this, "Report Sale") + " #" + Dictionary.getValue(this, "Daily"));
                    if (!"".equals(filterDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "Date") + " " + Utility.getPostParameterEmpty(this, "recapDate"));
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                            getController().setWhere("t.saleTimestamp BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));
                        } else {
                            getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));
                        }

                        getController().setPaginationFilter("&mode=" + mode + "&filter=" + filter + "&recapDate=" + filterDate);
                    }

                } else if ("2".equals(mode)) {
                    String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                    if (!"".equals(filterStartDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "From") + " " + Utility.getPostParameterEmpty(this, "startDate"));
                        getExportParams().put("title", Dictionary.getValue(this, "Report Sale") + " #" + Dictionary.getValue(this, "Weekly"));
                        DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                        Calendar cal = Calendar.getInstance();
                        cal.setTime((Date) dateFormatter.parse(filterStartDate));
                        String start = dateFormatter.format(cal.getTime());
                        cal.add(Calendar.DATE, 7);
                        String end = dateFormatter.format(cal.getTime());
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                            getController().setWhere("t.saleTimestamp BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        } else {
                            getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                        }

                        getController().setPaginationFilter("&mode=" + mode + "&filter=" + filter + "&startDate=" + filterStartDate);
                    }

                } else if ("3".equals(mode)) {
                    String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                    String filterMonth = Utility.getParameterEmpty(getRequest(), "recapMonth");
                    if (!"".equals(filterYear) && !"".equals(filterMonth)) {
                        getExportParams().put("filter", Utility.getPostParameterEmpty(this, "recapYear") + " - " + Dictionary.getValue(this, Utility.getLongMonthName(Integer.parseInt(Utility.getPostParameterEmpty(this, "recapMonth")))));
                        getExportParams().put("title", Dictionary.getValue(this, "Report Sale") + " #" + Dictionary.getValue(this, "Monthly"));
                        DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                        StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", filterMonth);
                        Utility.replaceAll(date, "dd", "01");
                        String start = date.toString();
                        Calendar cal = Calendar.getInstance();
                        cal.setTime((Date) dateFormatter.parse(start));
                        int max = cal.getActualMaximum(Calendar.DATE);
                        date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", filterMonth);
                        Utility.replaceAll(date, "dd", "" + max);
                        String end = date.toString();
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                            getController().setWhere("t.saleTimestamp BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                        } else {
                            getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                        }

                        getController().setPaginationFilter("&mode=" + mode + "&filter=" + filter +"&recapYear=" + filterYear + "&filterMonth=" + filterMonth);
                    }

                } else if ("4".equals(mode)) {
                    String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                    if (!"".equals(filterYear)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "Year") + " " + Utility.getPostParameterEmpty(this, "recapYear"));
                        getExportParams().put("title", Dictionary.getValue(this, "Report Sale") + " #" + Dictionary.getValue(this, "Yearly"));
                        DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                        StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", "01");
                        Utility.replaceAll(date, "dd", "01");
                        String start = date.toString();
                        date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", "12");
                        Utility.replaceAll(date, "dd", "31");
                        String end = date.toString();
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                            getController().setWhere("t.saleTimestamp BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        } else {
                            getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                        }

                        getController().setPaginationFilter("&mode=" + mode + "&filter=" + filter + "&recapYear=" + filterYear);

                    }

                } else if ("5".equals(mode)) {
                    String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                    String filterEndDate = Utility.getParameterEmpty(getRequest(), "endDate");
                    getExportParams().put("title", Dictionary.getValue(this, "Report Sale") + " #" + Dictionary.getValue(this, "Custom"));
                    if (!"".equals(filterStartDate) && "".equals(filterEndDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "From") + " " + Utility.getPostParameterEmpty(this, "startDate"));
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                            getController().setWhere("t.saleTimestamp >= :start");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        } else {
                            getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp >= :start");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        }
                    } else if ("".equals(filterStartDate) && !"".equals(filterEndDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "To") + " " + Utility.getPostParameterEmpty(this, "endDate"));
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                            getController().setWhere("t.saleTimestamp <= :end");
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        } else {
                            getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp <= :end");
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        }

                    } else if (!"".equals(filterStartDate) && !"".equals(filterEndDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "From") + " " + Utility.getPostParameterEmpty(this, "startDate") + " " + Dictionary.getValue(this, "to") + " " + Utility.getPostParameterEmpty(this, "endDate"));
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                            getController().setWhere("t.saleTimestamp BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        } else {
                            getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        }
                    }
                }

                datasource = new JRBeanArrayDataSource(getController().getData(true).toArray());
                JasperPrint jasperPrint = JasperFillManager.fillReport(getClass().getResourceAsStream("ReportSale.jasper"), getExportParams(), datasource);
                if ("2".equals(getCurrentUser().getUserExport())) {
                    JRXlsExporter excelExporter = new JRXlsExporter();
                    excelExporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
                    excelExporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, getResponse().getOutputStream());
                    excelExporter.exportReport();
                } else {
                    JasperExportManager.exportReportToPdfStream(jasperPrint, getResponse().getOutputStream());
                }
                return null;
            } catch (Exception e) {
                Logger.getInstance().log(e);
            }
            return new StringBuffer();

        } else {
            StringBuffer content = Utility.load(Bynga.BYNGA_PACKAGE_THEME + "content.html");
            Template template = new Template();
            template.loadTemplate(getClass().getResourceAsStream("/com/buanalintas/sandals/pos/reportsale/recap.html"));

            template.addValue(MESSAGE_DAILY, Dictionary.getValue(this, "Please enter date"));
            template.addValue(MESSAGE_WEEKLY, Dictionary.getValue(this, "Please enter start date"));
            template.addValue(MESSAGE_MONTHLY_YEAR, Dictionary.getValue(this, "Please select year"));
            template.addValue(MESSAGE_MONTHLY_MONTH, Dictionary.getValue(this, "Please select month"));
            template.addValue(MESSAGE_YEARLY, Dictionary.getValue(this, "Please select year"));
            template.addValue(MESSAGE_CUSTOM, Dictionary.getValue(this, "Please enter start date or end date or both"));

            template.addValue(SELECT_FILTER, Dictionary.getValue(this, "Select SPG"));
            String selectFilter = "<option value=\"*\">" + Dictionary.getValue(this, "All SPG") + "</option>";
            String filter = Utility.getParameterEmpty(getRequest(), "filter");
            try {
                List<PosMasterSales> spgs = getEntityManager().createQuery("SELECT t FROM PosMasterSales t ORDER BY t.salesName").setHint(Persistence.REFRESH_HINT, true).getResultList();
                for (PosMasterSales spg : spgs) {
                    selectFilter += "<option value=\"" + spg.getSalesName() + "\"" + (filter.equals("" + spg.getSalesName()) ? " selected" : "") + ">" + spg.getSalesName() + " - " + spg.getSalesUnitId() + "</option>";
                }
            } catch (Exception e) {
            }
            template.addValue(SELECT_FILTER_VALUES, selectFilter);

            template.addValue(SELECT_MODE, Dictionary.getValue(this, "Select mode"));
            template.addValue(SELECT_MODE_ONCHANGE, Dictionary.getValue(this, "'?m=" + getMenu().getMenuName() + "&a=" + getMenu().getMenuAction() + "&mode='+this.value"));

            String selectMode = "";
            String mode = Utility.getParameterEmpty(getRequest(), "mode");
            if ("".equals(mode)) {
                mode = "1";
            }
            selectMode += "<option value=\"1\"" + ("1".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Daily") + "</option>";
            selectMode += "<option value=\"2\"" + ("2".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Weekly") + "</option>";
            selectMode += "<option value=\"3\"" + ("3".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Monthly") + "</option>";
            selectMode += "<option value=\"4\"" + ("4".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Yearly") + "</option>";
            selectMode += "<option value=\"5\"" + ("5".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Custom") + "</option>";
            template.addValue(SELECT_VALUES, selectMode);

            boolean complete = false;
            if ("1".equals(mode)) {
                StringBuffer parameter = template.getEmptyElement(MODE_DAILY);
                Utility.replaceAll(parameter, DAILY_SELECT_DATE, Dictionary.getValue(this, "Date"));
                Utility.replaceAll(parameter, DAILY_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "recapDate"));
                template.addElement(MODE_DAILY, parameter);
                template.addElement(MODE_WEEKLY, null);
                template.addElement(MODE_MONTHLY, null);
                template.addElement(MODE_YEARLY, null);
                template.addElement(MODE_CUSTOM, null);
                String filterDate = Utility.getParameterEmpty(getRequest(), "recapDate");
                if (!"".equals(filterDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&recapDate=" + Utility.getPostParameterEmpty(this, "recapDate"));
                    template.addElement(RECAP_EXPORT, export);
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                        getController().setWhere("t.saleTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));
                    } else {
                        getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));
                    }

                    getController().setPaginationFilter("&mode=" + mode + "&filter=" + filter +"&startDate=" + "&recapDate=" + filterDate);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }
            } else if ("2".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                StringBuffer parameter = template.getEmptyElement(MODE_WEEKLY);
                Utility.replaceAll(parameter, WEEKLY_SELECT_START_DATE, Dictionary.getValue(this, "Start date"));
                Utility.replaceAll(parameter, WEEKLY_START_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "startDate"));
                template.addElement(MODE_WEEKLY, parameter);
                template.addElement(MODE_MONTHLY, null);
                template.addElement(MODE_YEARLY, null);
                template.addElement(MODE_CUSTOM, null);
                String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                if (!"".equals(filterStartDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&startDate=" + Utility.getParameterEmpty(getRequest(), "startDate"));
                    template.addElement(RECAP_EXPORT, export);

                    DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime((Date) dateFormatter.parse(filterStartDate));
                    String start = dateFormatter.format(cal.getTime());
                    cal.add(Calendar.DATE, 7);
                    String end = dateFormatter.format(cal.getTime());
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);

                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                        getController().setWhere("t.saleTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    } else {
                        getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    }

                    getController().setPaginationFilter("&mode=" + mode +"&filter=" + filter + "&startDate=" + "&startDate=" + filterStartDate);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }

            } else if ("3".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                template.addElement(MODE_WEEKLY, null);
                StringBuffer parameter = template.getEmptyElement(MODE_MONTHLY);
                Utility.replaceAll(parameter, MONTHLY_SELECT_YEAR, Dictionary.getValue(this, "Year"));
                String selectYear = "<option value=\"\">" + Dictionary.getValue(this, "Select year") + "</option>";
                int year = Calendar.getInstance().get(Calendar.YEAR);
                for (int i = year; i >= 2010; i--) {
                    selectYear += "<option value=\"" + i + "\"" + (("" + i).equals(Utility.getParameterEmpty(getRequest(), "recapYear")) ? " selected" : "") + ">" + i + "</option>";
                }
                Utility.replaceAll(parameter, MONTHLY_SELECT_YEAR_VALUES, selectYear);
                Utility.replaceAll(parameter, MONTHLY_SELECT_MONTH, Dictionary.getValue(this, "Month"));
                String selectMonth = "<option value=\"\">" + Dictionary.getValue(this, "Select month") + "</option>";
                for (int i = 1; i <= 12; i++) {
                    selectMonth += "<option value=\"" + i + "\"" + (("" + i).equals(Utility.getParameterEmpty(getRequest(), "recapMonth")) ? " selected" : "") + ">" + Dictionary.getValue(this, Utility.getLongMonthName(i)) + "</option>";
                }
                Utility.replaceAll(parameter, MONTHLY_SELECT_MONTH_VALUES, selectMonth);
                template.addElement(MODE_MONTHLY, parameter);
                template.addElement(MODE_YEARLY, null);
                template.addElement(MODE_CUSTOM, null);
                String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                String filterMonth = Utility.getParameterEmpty(getRequest(), "recapMonth");
                if (!"".equals(filterYear) && !"".equals(filterMonth)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&recapYear=" + Utility.getPostParameterEmpty(this, "recapYear") + "&recapMonth=" + Utility.getPostParameterEmpty(this, "recapMonth"));
                    template.addElement(RECAP_EXPORT, export);

                    DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                    StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", filterMonth);
                    Utility.replaceAll(date, "dd", "01");
                    String start = date.toString();
                    Calendar cal = Calendar.getInstance();
                    cal.setTime((Date) dateFormatter.parse(start));
                    int max = cal.getActualMaximum(Calendar.DATE);
                    date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", filterMonth);
                    Utility.replaceAll(date, "dd", "" + max);
                    String end = date.toString();
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);

                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                        getController().setWhere("t.saleTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    } else {
                        getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    }

                    getController().setPaginationFilter("&mode=" + mode + "&filter=" + filter + "&startDate=" + "&recapYear=" + filterYear + "&filterMonth=" + filterMonth);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }

            } else if ("4".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                template.addElement(MODE_WEEKLY, null);
                template.addElement(MODE_MONTHLY, null);
                StringBuffer parameter = template.getEmptyElement(MODE_YEARLY);
                Utility.replaceAll(parameter, YEARLY_SELECT_YEAR, Dictionary.getValue(this, "Year"));
                String selectYear = "<option value=\"\">" + Dictionary.getValue(this, "Select year") + "</option>";
                int year = Calendar.getInstance().get(Calendar.YEAR);
                for (int i = year; i >= 2010; i--) {
                    selectYear += "<option value=\"" + i + "\"" + (("" + i).equals(Utility.getParameterEmpty(getRequest(), "recapYear")) ? " selected" : "") + ">" + i + "</option>";
                }
                Utility.replaceAll(parameter, YEARLY_SELECT_YEAR_VALUES, selectYear);
                template.addElement(MODE_YEARLY, parameter);
                template.addElement(MODE_CUSTOM, null);
                String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                if (!"".equals(filterYear)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&recapYear=" + Utility.getPostParameterEmpty(this, "recapYear"));
                    template.addElement(RECAP_EXPORT, export);
                    DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                    StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", "01");
                    Utility.replaceAll(date, "dd", "01");
                    String start = date.toString();
                    date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", "12");
                    Utility.replaceAll(date, "dd", "31");
                    String end = date.toString();
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                        getController().setWhere("t.saleTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                    } else {
                        getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    }

                    getController().setPaginationFilter("&mode=" + mode + "&filter=" + filter + "&startDate=" + "&recapYear=" + filterYear);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }

            } else if ("5".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                template.addElement(MODE_WEEKLY, null);
                template.addElement(MODE_MONTHLY, null);
                template.addElement(MODE_YEARLY, null);
                String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                String filterEndDate = Utility.getParameterEmpty(getRequest(), "endDate");
                if (!"".equals(filterStartDate) && "".equals(filterEndDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&startDate=" + Utility.getPostParameterEmpty(this, "startDate") + "&endDate=" + Utility.getPostParameterEmpty(this, "endDate"));
                    template.addElement(RECAP_EXPORT, export);

                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                        getController().setWhere("t.saleTimestamp >= :start");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));

                    } else {
                        getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp >= :start");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                    }

                    complete = true;
                } else if ("".equals(filterStartDate) && !"".equals(filterEndDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&startDate=" + Utility.getPostParameterEmpty(this, "startDate") + "&endDate=" + Utility.getPostParameterEmpty(this, "endDate"));
                    template.addElement(RECAP_EXPORT, export);

                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                        getController().setWhere("t.saleTimestamp <= :end");
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));

                    } else {
                        getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp <= :end");
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    }

                    complete = true;
                } else if (!"".equals(filterStartDate) && !"".equals(filterEndDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&startDate=" + Utility.getPostParameterEmpty(this, "startDate") + "&endDate=" + Utility.getPostParameterEmpty(this, "endDate"));
                    template.addElement(RECAP_EXPORT, export);

                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "filter"))) {
                        getController().setWhere("t.saleTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    } else {
                        getController().setWhere("t.saleSalesId.salesName='" + filter + "' AND t.saleTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    }

                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }
                getController().setPaginationFilter("&mode=" + mode + "&filter=" + filter + "&startDate=" + filterStartDate + "&endDate=" + filterEndDate);
                StringBuffer parameter = template.getEmptyElement(MODE_CUSTOM);
                Utility.replaceAll(parameter, CUSTOM_SELECT_START_DATE, Dictionary.getValue(this, "Start date"));
                Utility.replaceAll(parameter, CUSTOM_START_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "startDate"));
                Utility.replaceAll(parameter, CUSTOM_SELECT_END_DATE, Dictionary.getValue(this, "End date"));
                Utility.replaceAll(parameter, CUSTOM_END_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "endDate"));
                template.addElement(MODE_CUSTOM, parameter);
            }
            template.addValue(BUTTON_SEARCH, Dictionary.getValue(this, "Search"));
            int record = getController().getRecordCount();
            template.addValue(RECORD, Dictionary.getValue(this, "Total all record") + ": " + NumberFormatter.getInstance().format(record));
            if (record > 10000000) {
                template.addValue(WARNING, "return confirm('" + Dictionary.getValue(this, "Records is to big need long time to process, approximately") + " " + NumberFormatter.getInstance().format(record) + " " + Dictionary.getValue(this, "records, are you sure want to continue?") + "');");
            } else {
                template.addValue(WARNING, "");
            }
            if (complete) {
                template.addValue(LIST_DATA, PageList.process(this).toString());
            } else {
                template.addValue(LIST_DATA, "-- " + Dictionary.getValue(this, "Please select parameter") + " --");
            }
            Utility.replaceAll(content, Template.CONTENT_CONTENT, template.process().toString());
            updateContent(content, getMenu(), true);
            return content;
        }
    }
}
